package cn.tradewin.advisor.pages.security;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Roles;
import cn.tradewin.advisor.persist.services.AdvisorSecurityService;

public class UpdRole {

	@Inject
	private AdvisorSecurityService securityService;

	@Property
	private Roles role;

	private String id;

	@Inject
	private Logger log;

	void onActivate(String id) throws DBException {
		this.id = id;
	}

	String onPassivate() {
		return id;
	}
	
	@OnEvent(EventConstants.PREPARE_FOR_RENDER)
	void onPrepareForRender() throws DBException {
		role = securityService.findRole(id);
	}

	@OnEvent(EventConstants.PREPARE_FOR_SUBMIT)
	void onPrepareForSubmit() {
		if (role == null) {
			role = new Roles();
		}
	}
	
	@Log
	Object onSubmit() {
		try {
			securityService.updRole(role);
		} catch (Exception e) {
			log.debug(e.getMessage());
		}
		return RoleBoard.class;
	}	
}
